package com.huiyujinrong.qiangdanbao.activity.my;


import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.bean.FaceFindBean;
import com.huiyujinrong.qiangdanbao.bean.UserInforBean;
import com.huiyujinrong.qiangdanbao.utils.SharedHelper;

import java.net.URLEncoder;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserInforActivity extends AppCompatActivity implements UserInforContacrt.View {

    @BindView(R.id.toolbar_Title)
    TextView title;
    @BindView(R.id.toolbar_Back)
    RelativeLayout back;

    @BindView(R.id.if_name)
    TextView name;
    @BindView(R.id.if_jigou)
    TextView jigou;
    @BindView(R.id.if_city)
    TextView city;
    @BindView(R.id.if_idNumber)
    TextView idNumber;
    @BindView(R.id.ui_status)
    TextView status;

    @BindView(R.id.id_status)
    TextView id_status;
    @BindView(R.id.work_status)
    TextView work_status;

    @BindView(R.id.tvFaces)
    TextView tvFaceRecord;
    private int status_num;

    private Toast t;

    private UserInforBean u_bean;

    private UserInforPresenter presenter;
    private boolean hasFaceRecord = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.information);
        ButterKnife.bind(this);
        inSetToolBar();
        Intent intent = getIntent();
        status_num = intent.getIntExtra("status_num", 0);

        showStatus(status_num);

        presenter = new UserInforPresenter(this);
        presenter.start();

        presenter.getUserInfor(String.valueOf(SharedHelper.get(this, "user_id", "")));
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.findZmxyFaceRecordByUserId(String.valueOf(SharedHelper.get(this, "user_id", "")));
    }

    private void inSetToolBar() {
        back.setVisibility(View.VISIBLE);
        title.setText("信贷经理认证");
    }

    /**
     * 认证状态
     *
     * @param status_num
     */
    private void showStatus(int status_num) {
        switch (status_num) {
            case 2:
                status.setText("审核中");
                break;
            case 3:
                status.setText("已认证");
                break;
        }
    }

    @OnClick(R.id.toolbar_Back)
    void onBack() {
        finish();
    }

    //身份证
    @OnClick(R.id.ui_id)
    void ui_ID() {
        if (u_bean == null) {

        } else {
            Intent intentID = new Intent(this, SelectID.class);
            intentID.putExtra("id_just", u_bean.getIdcard_front());
            intentID.putExtra("id_back", u_bean.getIdcard_back());
            startActivity(intentID);
        }

    }

    //工作证明
    @OnClick(R.id.ui_work)
    void ui_WORK() {
        if (u_bean == null) {
            return;
        } else {
            Intent intentWORK = new Intent(this, SelectWork.class);
            intentWORK.putExtra("work_gongpai", u_bean.getWork_card());
            intentWORK.putExtra("work_mingpian", u_bean.getBusiness_card());
            startActivity(intentWORK);
        }

    }

    @OnClick({R.id.llFaces})
    public void onClick() {
        if (hasFaceRecord) {
            showToast("人脸识别已经通过啦`");
            return;
        }
        presenter.getZmxyFaceEngine(String.valueOf(SharedHelper.get(this, "user_id", "")));
    }

    @Override
    public void showToast(String str) {
        if (t == null) {
            t = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        } else {
            t.setText(str);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        t.show();
    }

    @Override
    public void getUserInforData(UserInforBean bean) {
        if (bean == null) {
            name.setText("未填写");
            jigou.setText("未填写");
            city.setText("未选择");
            idNumber.setText("未填写");
            id_status.setText("暂无照片");
            work_status.setText("暂无照片");
        } else {
            u_bean = bean;
            name.setText(bean.getTrue_name());
            jigou.setText(bean.getOrganize_name());
            city.setText(bean.getAd_path());
            idNumber.setText(bean.getIdcard_no());
            id_status.setText("已上传");
            work_status.setText("已上传");
        }
    }

    @Override
    public void findZmxyFaceRecord(FaceFindBean code) {
        if (code != null && code.getFace_flag() == 1) {
            tvFaceRecord.setText("已认证");
            hasFaceRecord = true;
        } else {
            tvFaceRecord.setText("去认证");
            hasFaceRecord = false;
        }
    }

    /**
     * 人脸识别回调
     *
     * @param zmxy_face_url
     */
    @Override
    public void onFaceEngineResult(String zmxy_face_url) {
        if (zmxy_face_url != null) {
            doVerify(zmxy_face_url);
        }
    }

    private void doVerify(String url) {
        if (hasApplication()) {
            Intent action = new Intent(Intent.ACTION_VIEW);
            StringBuilder builder = new StringBuilder();
            // 这里使用固定appid 20000067
            builder.append("alipays://platformapi/startapp?appId=20000067&url=");
            builder.append(URLEncoder.encode(url));
            action.setData(Uri.parse(builder.toString()));
            startActivity(action);
        } else {
            new AlertDialog.Builder(this).
                    setTitle("提示").
                    setMessage("是否下载并安装支付宝完成认证?")
                    .setPositiveButton("好的", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent action = new Intent(Intent.ACTION_VIEW);
                            action.setData(Uri.parse("https://m.alipay.com"));
                            startActivity(action);
                        }
                    }).setNegativeButton("算了", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            }).create().show();

        }
    }


    /**
     * 判断是否安装了支付宝
     *
     * @return true 为已经安装
     */
    private boolean hasApplication() {
        PackageManager manager = getPackageManager();
        Intent action = new Intent(Intent.ACTION_VIEW);
        action.setData(Uri.parse("alipays://"));
        List list = manager.queryIntentActivities(action, PackageManager.GET_RESOLVED_FILTER);
        return list != null && list.size() > 0;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.end();
    }
}
